//
//  MiniCircleCollectionViewCell.m
//  Liuhe
//
//  Created by zr on 2018/8/13.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "MiniCircleCollectionViewCell.h"

@interface MiniCircleCollectionViewCell ()

@property (nonatomic, weak)UIImageView * backImageView;
@property (nonatomic, weak)UIButton * likeButton;
@property (nonatomic, weak)UILabel * likeLabel;
@property (nonatomic, weak)UILabel * nameLabel;
@property (nonatomic, weak)UILabel * ageLabel;
@property (nonatomic, weak)UILabel * timeLabel;

//@property (nonatomic, weak)UILabel * contentLabel;
//@property (nonatomic, weak)UIImageView * backImageView;
//@property (nonatomic, weak)UIImageView * sayImageView;
//@property (nonatomic, weak)UILabel * sayLabel;
//@property (nonatomic, weak)UIView * lineView;
@property (nonatomic, copy)NSString * user_id;

@end

@implementation MiniCircleCollectionViewCell

+ (NSString *)cellIdentifier {
    return @"MiniCircleCell";
}


- (void)setModel:(MakeFriendsModel *)model{
    
    _model = model;
    
    self.user_id = model.user_id;
    self.nameLabel.text = model.name;
    self.ageLabel.text = [NSString stringWithFormat:@"%@岁",model.age];
    self.likeLabel.text = [NSString stringWithFormat:@"%@",model.praise];
    // timeStampString 是服务器返回的13位时间戳
    NSString *timeStampString  = model.ctime;
    
    // iOS 生成的时间戳是10位
    NSTimeInterval interval    =[timeStampString doubleValue];
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM-dd HH:mm"];
    NSString *dateString       = [formatter stringFromDate: date];
    
    self.timeLabel.text = [NSString stringWithFormat:@"来自%@  %@",model.address,dateString];
    
    NSURL * url = [NSURL URLWithString:model.headimg.firstObject];
    
    [self.backImageView sd_setImageWithURL:url];
    
    int ispraise = [model.ispraise intValue];
    if (ispraise == 0) {
        
        self.likeButton.selected = NO;
    }else{
        
        self.likeButton.selected = YES;
    }
}

+ (instancetype)cellWithCollectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexPath {

    MiniCircleCollectionViewCell *cell = (MiniCircleCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:[MiniCircleCollectionViewCell cellIdentifier] forIndexPath:indexPath];
    
    return cell;
}


- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        self.contentView.backgroundColor = ColorWithRGB(0xF2F2F2);
        self.contentView.layer.cornerRadius = 5;
        self.contentView.layer.masksToBounds = YES;
//        self.contentView.layer.borderColor = [UIColor blackColor].CGColor;
//        self.contentView.layer.borderWidth = 0.4;
        
//        [self.contentView addSubview:self.backImageView];
//
//        [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.edges.mas_equalTo(self.contentView);
//        }];
//
//        [self.contentView addSubview:self.labelRecord];
//        [self.labelRecord mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.center.mas_equalTo(self.contentView);
//        }];
        
        [self setupUI];
    }
    return self;
}


- (void)setupUI{
    
    UIImageView * backImageView = [UIImageView new];
    
    backImageView.backgroundColor = [UIColor lightGrayColor];
    
    self.backImageView = backImageView;
    
    [self.contentView addSubview:self.backImageView];
    
    
    UIButton * likeButton = [UIButton new];
    
    [likeButton setImage:ImageNamed(@"ihii232") forState:UIControlStateNormal];
    [likeButton setImage:ImageNamed(@"xinxin1321") forState:UIControlStateSelected];
    [likeButton addTarget:self action:@selector(clickLikeButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.likeButton = likeButton;
    
    [self.contentView addSubview:self.likeButton];
    
    
    UILabel * likeLabel = [UILabel new];
    
    likeLabel.textColor = WHITE_COLOR;
    likeLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    likeLabel.text = @"----";
    
    self.likeLabel = likeLabel;
    
    [self.contentView addSubview:self.likeLabel];
    
    
    UILabel * nameLabel = [UILabel new];
    
    nameLabel.textColor = BLACK_COLOR;
    nameLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    nameLabel.text = @"----";
    
    self.nameLabel = nameLabel;
    
    [self.contentView addSubview:self.nameLabel];
    
    
    UILabel * ageLabel = [UILabel new];
    
    ageLabel.textColor = [UIColor grayColor];
    ageLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    ageLabel.text = @"----";
    
    self.ageLabel = ageLabel;
    
    [self.contentView addSubview:self.ageLabel];
    
    
    UILabel * timeLabel = [UILabel new];
    
    timeLabel.textColor = [UIColor grayColor];
    timeLabel.font = [UIFont systemFontOfSize:11 * SCREENH6];
    timeLabel.text = @"----";
    
    self.timeLabel = timeLabel;
    
    [self.contentView addSubview:self.timeLabel];
}


- (void)layoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.mas_equalTo(self.contentView);
        make.height.mas_equalTo(240 * SCREENH6);
    }];
    
    [self.likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.contentView).offset(-10 * SCREENH6);
        make.top.mas_equalTo(self.contentView).offset(15 * SCREENH6);
        make.height.width.mas_equalTo(35 * SCREENH6);
    }];
    
    [self.likeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.likeButton);
        make.top.mas_equalTo(self.likeButton.mas_bottom).offset(7 * SCREENH6);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).offset(10 * SCREENH6);
        make.top.mas_equalTo(self.backImageView.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.ageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.nameLabel);
        make.leading.mas_equalTo(self.nameLabel.mas_trailing).offset(15 * SCREENH6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(5 * SCREENH6);
        make.leading.mas_equalTo(self.nameLabel);
    }];
}


- (void)clickLikeButton{
    
    if (!self.likeButton.selected) {
        
        NSString * user_id = DEF_GET_OBJECT(@"user_id");
        
        NSDictionary * dict = @{@"type":@"1",@"user_id":user_id,@"other_user_id":self.user_id};
        
        [[NetworkTool sharedTools] POST:@"/index/Liuheonline/favor" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
          
            NSLog(@"===%@",responseObject);
            
            int status = [responseObject[@"status"] intValue];
            
            if (status == 200) {
                
                self.likeButton.selected = YES;
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
    }
    
    
}

@end
